//保存表格的实例对象
var sx = null;

//封装方法重载表格
function sxTable(){
    sx.reload()
}

layui.use(['table','form'], function(){
    var table = layui.table;
    var form = layui.form;

    //提交筛选查询
    form.on('submit(*)',function (data){
        var data = data.field;
        console.log(data)
        sx.reload({
            url:'/getStusByLikeIdAndName',
            where:{
                studentId:data.studentId,
                studentName:data.studentName,
                status:1
            }
        },true)
        return false
    })

    //提交数据重置为空
    form.on('submit(cz)',function (data){

        $("[name=studentId]").val(undefined)
        $("[name=studentName]").val(undefined)

        sx.reload({
            url:'/getStuOperationLogByOne',
        },true)
        return false
    })


    //第一个实例
    sx = table.render({
        elem: '#demo'
        ,url: '/getStuOperationLogByOne' //数据接口
        , height:350
        ,page:{
            layout:['count','prev','page','next','limit'],
            groups:5,
            limit:10,
            limits:[3,5,10]
        }
        ,cols: [
            [
                {checkbox: true},
                {field: 'studentId', title: '学号', width:200,align: 'center'},
                {field: 'studentName', title: '姓名', width:200,align: 'center'},
                {field: 'operationRemark', title: '操作备注', width:270,align: 'center'},
                {field: 'operationDateTime', title: '操作时间', width:230,align: 'center'},
                {title: '操作',toolbar:'#tooldemo',width: 200,align: 'center'}
            ]
        ],
        defaultToolbar: ['filter'],
        toolbar:'#toolbardemo',
        parseData:function (res){
            return{
                "code":res.state,
                "msg":res.msg,
                "count":res.total,
                "data":res.data
            }
        }
    })


    //监听头部
    table.on('toolbar(stuTable)',function (obj){
        var event = obj.event
        var checkStatus = table.checkStatus(obj.config.id);

       if(event == 'dc'){

           var data = checkStatus.data;
           console.log(checkStatus)
           // 判断是否选中数据
           if (checkStatus.data.length === 0) {
               layer.msg('请选择数据', {
                   icon: 2
               })
           } else {
               //浏览器下载导出文件
               table.exportFile(sx.config.id,data, 'xlsx');
               // 导出文件（发送后台导出数据）
               // $.ajax({
               //     type:'post',
               //     url:'/dcStuOperationLog',
               //     data:JSON.stringify(data),
               //     dataType:'json',
               //     contentType:'application/json;charset=utf-8',
               //     success:function (res){
               //         if (res.state == '200'){
               //             layer.msg('导出成功',{icon:6,time:1000});
               //         }
               //     }
               // })

           }

        }else if(event == 'dy'){

            // 监听打印选中行
           var checkStatus = table.checkStatus('demo')
            console.log(checkStatus)
            // 判断是否选中数据
            if (checkStatus.data.length === 0) {
                layer.msg('请选择数据', {
                    icon: 2
                })
            } else {
                printSelect(checkStatus.data)
            }
        }
    }),

    //tool 监听工具条
    table.on('tool(stuTable)',function(obj){

       if(obj.event == 'del'){

            layer.confirm('是否确认删除',function (index){
                //发送ajax
                $.ajax({
                    type: 'post',
                    url: '/delStuOperationLog',
                    data: {"id":obj.data.logId},
                    dataType: 'json',
                    success: function (rs) {
                        if (rs.state == 200){

                            layer.msg("删除操作日志信息成功",{time: 700,icon:6},function (){
                                sxTable();
                            })
                        }else {
                            layer.msg("删除操作日志信息失败",{time: 1000,icon:5});
                        }
                    },
                    error: function (e) {
                        console.log("请求异常");
                        console.log(e);
                    }
                })
            })
        }
    })
})


/*
   * [自定义打印选中行数据]
   * @param  {[type]}  allData [传入选中行]
   */
function printSelect(allData) {
    //用于包含内容
    var v = document.createElement("div");
    //页面头部，导入css  ，media="print"表示打印时使用该样式
    var f = ["<head>", "<style>",
        "div{font-size:22px; font-weight: 400;}",
        " th{height: 40px;}",
        ".main{width: 100%; height: 95%}",
        "</style>", "</head>"
    ].join("");
    var contentHtml = "";
    for (let i = 0; i < allData.length; i++) {
        var data = allData[i]
        var template =
            "<div class='main'>" +
            "<h1 style='margin: 0 auto;width: 270px;padding-bottom: 30px'>" +
            "进思学生信息</h1>"+
            "<table border='1' cellspacing='0' style='margin: 0 auto; width: 1100px;'>"+
            "<tr>"+
            "<th>学号</th>"+
            "<th>姓名</th>"+
            "<th>身份证号</th>"+
            "</tr>"+

            "<tr>"+
            "<th>" + data.studentId + "</th>"+
            "<th>" + data.studentName + "</th>"+
            "<th>" + data.studentIdNumber + "</th>"+
            "</tr>"+

            "</table>"+
            "<br/>"+
            "<table border='1' cellspacing='0' style='margin: 0 auto; width: 1100px;'>"+
            "<tr>"+
            "<th>班级</th>"+
            "<th>学期</th>"+
            "<th>入学日期</th>"+
            "</tr>"+

            "<tr>"+
            "<th>" + data.classesName + "</th>"+
            " <th>" + data.studentSchoolTerm + "</th>"+
            "<th>" + data.studentCreateDate + "</th>"+
            "</tr>"+
            "</table>"+
            "</div>"
        contentHtml += template;
    }

    //contentHtml为已拼凑好的内容
    $(v).append(contentHtml)
    //新建窗口
    var h = window.open("打印窗口", "_blank");
    //写入拼凑内容
    h.document.write(f + $(v).prop("outerHTML"));
    //调用打印
    h.print();
    //关闭页面
    h.close();
}